// pages/orderdetail/orderdetail.js
var config = require('../../config.js');
var request = require('../../utils/request.js');
var app = getApp();
var payutil = require('../../utils/pay.js');
var express = require('../../utils/express.js');

const orderStateBgImgs = [
  {
    state: 1,
    img: 'https://image.hongheyu1688.com/miniapp/orderstate_topay.png'
  }, {
    state: 2,
    img: 'https://image.hongheyu1688.com/miniapp/orderstate_tosend.png'
  }, {
    state: 3,
    img: 'https://image.hongheyu1688.com/miniapp/orderstate_toreceive.png'
  }, {
    state: 4,
    img: 'https://image.hongheyu1688.com/miniapp/orderstate_complete.png'//完成
  }]

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    orderState: 0,//订单状态
    orderInfo: {},
    order_state: '',//订单状态显示字符串
    tradeInfo: {},//订单信息
    goods: [],//商品
    isIpx: app.globalData.isIpx,
    expressDetail: [],//物流信息
    expressStr: '',//物流字符串
    expressCompany: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.orderid;
    this.setData({
      orderId: id
    });
    this.getOrderDetail();
  },

  /**
   * 获取订单详情
   */
  getOrderDetail: function () {
    var url = config.orderDetailUrl;
    let params = {
      orderId: this.data.orderId
    };
    var self = this;
    request.requestPost(url, params)
      .then(data => {
        console.log(data);

        self.setData({
          orderInfo: data.detail,
          tradeInfo: data.trade,
          goods: data.goods,
          orderState: parseInt(data.detail.STATE)
        });

        if (data.detail.expressNo) {
          self.expressDetailAction();
        }

        switch (this.data.orderState) {
          case 1:
            self.setData({
              order_state: '等待买家付款'
            });
            break;
          case 2:
            self.setData({
              order_state: '等待商家发货'
            });
            break;
          case 3:
            self.setData({
              order_state: '等待买家收货'
            });
            break;
          case 6:
            self.setData({
              order_state: '订单已取消'
            })
            break;
        }

      }).catch(msg => {
        console.log(msg);
      });
  },


  payAction: function () {
    var tradeId = this.data.tradeInfo.id;
    payutil.payByTradeIdAction(tradeId)
      .then(res => {
        wx.redirectTo({
          url: '../pay/paysuccess',
        });
      }).then(res => {
        console.log(msg);
      }).catch(msg => {
        wx.hideToast('支付失败');
        wx.navigateBack({});
      })
  },

  /**
   * 取消订单
   */
  cancelOrderAction: function () {
    let self = this;
    wx.showModal({
      title: '提示',
      content: '确认取消订单吗?',
      success: function (res) {
        if (res.confirm) {
          let url = config.orderCancelUrl;
          let params = {
            orderId: self.data.orderId
          }
          request.requestPost(url, params)
            .then(data => {
              wx.navigateBack({
              });
            }).catch(msg => {
            });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },

  onRemindAction: function () {
    wx.showToast({
      title: '已提醒商户',
    })
  },

  onReceiveAction: function () {
    var url = config.finishOrderUrl;
    var params = {
      orderId: this.data.orderId
    };
    request.requestPost(url, params)
      .then(data => {
        wx.showToast({
          title: '已确认收货',
        })
      }).catch(msg => {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      });
  },

  /**
   * 物流详情
   */
  expressDetailAction: function () {
    var self = this;
    express.expressDetailAction(this.data.orderId)
      .then(data => {
        self.setData({
          expressStr: JSON.stringify(data),
          expressDetail: JSON.parse(data.detail),
          expressCompany: data.company
        });
      }).catch(msg => {
        console.log(msg);
      });
  },


  /**
   * 物流信息
   */
  expressAction: function () {
    wx.navigateTo({
      url: '../express/express?orderid=' + this.data.orderId,
    });
  }


})